package nl.sportan.samenloop.adapter;

import java.util.ArrayList;

import nl.sportan.samenloop.PhotoListActivity;
import nl.sportan.samenloop.R;
import nl.sportan.samenloop.communication.PhotoTarget;
import nl.sportan.samenloop.communication.flickr.PhotoDataRetriever;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gmail.yuyang226.flickr.photos.Photo;
import com.gmail.yuyang226.flickr.photos.Size;
import com.gmail.yuyang226.flickr.photosets.Photoset;

public class ImagePageAdapter extends PagerAdapter {

	private ArrayList<Photoset> photosets;

	private final Context context;
	private Handler handler;

	public ImagePageAdapter(Context context, ArrayList<Photoset> photosets) {
		this.context = context;
		this.photosets = photosets;
		handler = new Handler();
	}

	private View createImageView(View pager, int position) {
		View v = (LinearLayout) LayoutInflater.from(context).inflate(
				R.layout.image_view, ((ViewPager) pager), false);

		ImageView iv = (ImageView) v.findViewById(R.id.image);

		Photoset ps = photosets.get(position);

		iv.setOnClickListener(new PhotosetListener(ps));

		Photo p = ps.getPrimaryPhoto();
		String title = ps.getTitle();

		TextView titleView = (TextView) v.findViewById(R.id.title);
		titleView.setText(title);

		// FIXME This is wrong, very wrong!! Change to Thread workers!
		Thread t = new Thread(new PhotoDataRetriever(context, p, Size.MEDIUM,
				new PhotoHolder(iv), true));
		t.start();

		return v;
	}

	@Override
	public int getCount() {
		return photosets.size();
	}

	@Override
	public Object instantiateItem(View pager, int position) {
		View v = createImageView(pager, position);
		((ViewPager) pager).addView(v, 0);

		return v;
	}

	@Override
	public void destroyItem(View pager, int position, Object view) {
		((ViewPager) pager).removeView((LinearLayout) view);
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view.equals(object);
	}

	@Override
	public void finishUpdate(View view) {
	}

	@Override
	public void restoreState(Parcelable p, ClassLoader c) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View view) {
	}

	private class PhotoHolder implements PhotoTarget {

		private ImageView target;

		public PhotoHolder(ImageView target) {
			this.target = target;
		}

		public void setPhoto(Bitmap bitmap) {
			handler.post(new PhotoUpdater(bitmap, target));
		}

	}

	private class PhotoUpdater implements Runnable {

		private Bitmap bitmap;
		private ImageView target;

		public PhotoUpdater(Bitmap bitmap, ImageView target) {
			this.bitmap = bitmap;
			this.target = target;
		}

		public void run() {
			target.setImageBitmap(bitmap);
		}

	}

	private class PhotosetListener implements OnClickListener {

		private Photoset photoset;

		public PhotosetListener(Photoset photoset) {
			this.photoset = photoset;
		}

		public void onClick(View v) {
			Intent i = new Intent(context, PhotoListActivity.class);
			i.putExtra("PhotosetId", photoset.getId());

			context.startActivity(i);
		}

	}

}